import util from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    option1: [
      { text: '上海市', value: '上海市' }
    ],
    option2: [
      {text:'黄埔区',value:'黄埔区'},
      {text:'徐汇区',value:'徐汇区'},
      {text:'长宁区',value:'长宁区'},
      {text:'静安区',value:'静安区'},
      {text:'普陀区',value:'普陀区'},
      {text:'虹口区',value:'虹口区'},
      {text:'杨浦区',value:'杨浦区'},
      {text:'闵行区',value:'闵行区'},
      {text:'宝山区',value:'宝山区'},
      {text:'嘉定区',value:'嘉定区'},
      {text:'浦东新区',value:'浦东新区'},
      {text:'金山区',value:'金山区'},
      {text:'松江区',value:'松江区'},
      {text:'青浦区',value:'青浦区'},
      {text:'奉贤区',value:'奉贤区'},
      {text:'崇明区',value:'崇明区'}
    ],
    currentProvince:'上海市',
    currentPrepecture:'浦东新区'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        that.setData({lat:latitude,lng:res.longitude},()=>{
          that.searchInstStations({ province:that.data.currentProvince,prepecture:that.data.currentPrepecture});
        })

      }
    })

  },
  searchInstStations(params){
    let calcDistance=function(lat, lng) {
      let rad = function(d){//计算角度
        return d * Math.PI / 180.0;
      }
      let lat1 = rad(that.data.lat);
      let lat2 = lat * Math.PI / 180.0;
      let lat3 = lat1 - lat2;
      let Lb3 =rad(that.data.lng) -rad( lng );
      let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(lat3 / 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(Lb3 / 2), 2)));
      s = s * 6378.137;
      s = Math.round(s * 10000) / 10000;
      return  s.toFixed(1);
    }
    let that=this;
    params.lat=that.data.lat;
    params.lng=that.data.lng;
    util.promise('/searchInspectionStations',params
    ).then(res=>{
      if(res.data){
        // res.data.forEach(function (row){
        //   if( row.lat && row.lng ){
        //     row.distance = calcDistance(row.lat,row.lng );
        //   }
        // })
        // util.sort(res.data,'distance');
        that.setData({inspStations:res.data});
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  setSelectedInspectionStation(event){
    let inspectionStation = event.currentTarget.dataset.inspectionStation;
    let inspStations=this.data.inspStations;
    inspStations.forEach(function (inspStation) {
      if(inspStation.inspStationId===inspectionStation.inspStationId){
        inspStation.isSelected=!inspStation.isSelected;
      }else{
        inspStation.isSelected=false;
      }
    });
    this.setData({inspStations:inspStations});
  },
  selectInspectionStation(event){
    let that=this;
    let inspStations=that.data.inspStations;
    inspStations.forEach(function (inspStation) {
      if( inspStation.isSelected){
        inspStation.isSelected=!inspStation.isSelected;
        const eventChannel = that.getOpenerEventChannel();
        eventChannel.emit('acceptDataFromInspectionStationPage', inspStation);
        wx.navigateBack({ delta: 1 });
      }
    });
  },
  back(event){
    wx.navigateBack({ delta: 1 });
  },
  showRemak(event){
    let inspectionStation = event.currentTarget.dataset.inspectionStation;
    let inspStations=this.data.inspStations;
    inspStations.forEach(function (inspStation) {
      if(inspStation.inspStationId===inspectionStation.inspStationId){
        inspStation.isShowRemark=!inspStation.isShowRemark;
      }else{
        inspStation.isShowRemark=false;
      }
    });
    this.setData({inspStations:inspStations});
  },
  changeProvince(event){
    this.searchInstStations({province:event.detail,prepecture:this.data.currentPrepecture});
  },
  changePrepecture(event){
    this.searchInstStations({province:this.data.currentProvince,prepecture:event.detail});
  }
});